package com.sxm.yiwei.pinche;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.services.help.Tip;
import com.amap.api.services.route.RouteSearch;
import com.bigkoo.pickerview.TimePickerView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.pinche.OrderInProcessAdapter;
import com.sxm.yiwei.adapters.pinche.PincheOrderListAdapter;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.Order;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.utils.DateUtil;
import com.sxm.yiwei.utils.HandleDataUtil;
import com.sxm.yiwei.views.CenterDialog;
import com.sxm.yiwei.views.MyListview;

import java.util.ArrayList;


public class PincheActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private RelativeLayout rl_back;
    private TextView tv_passenger_tab;
    private TextView tv_host_tab;
    private TextView tv_release;
    private TabLayout tabLayout;
    private TextView tv_start_city;
    private TextView tv_end_city;
    private ImageView iv_change_direction;
    private TextView tv_date;
    private TextView tv_week;
    private TextView tv_start_time;
    private Button btn_search;
    private MyListview listView;
    private LinearLayout ll_choose_date;
    private int identity_flag=Constant.PASSENGER;//1代表乘客，2代表车主
    private String tabs[]={"长途","同城","旅游","长途急件"};
    private CenterDialog centerDialog;
    private LinearLayout ll_exchange_pos;
    private RelativeLayout rl_left;
    private RelativeLayout rl_right;
    private WindowManager mWindowManager;
    private int[] mLeftLocation;
    private int[] mRightLocation;
    private Bitmap mLeftCacheBitmap;
    private Bitmap mRightCacheBitmap;
    private TextView tv_end_time;
    private TextView tv_order;
    private RouteSearch routeSearch;
    private ImageView iv_left;
    private ImageView iv_right;
    private ArrayList<Order> beanList=new ArrayList<>();
    private int go_type =Constant.LONG;
    private OrderInProcessAdapter adapter;
    private PullToRefreshScrollView refreshView;
    private int mPosition;
    private RelativeLayout rl_building;


    @Override
    public void setContentView() {
        mWindowManager = getWindowManager();
        setContentView(R.layout.activity_pinche);
    }

    @Override
    public void initData() {
        //初始化tab
        for (int i = 0; i < tabs.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText(tabs[i]);
            tabLayout.addTab(tab);
        }
        //初始化拼车类型，默认选中第一条
        //初始化出发到达城市,设定必须有默认城市
        //初始化时间，默认明天
        //初始化列表
        adapter = new OrderInProcessAdapter(this,beanList);

        listView.setAdapter(adapter);
        getRelativeOrders();
        adapter.setOnOrderHandleListener(new OrderInProcessAdapter.OnOrderHandleListener() {
            @Override
            public void onStartService(String order_id, String from,int position) {
                mPosition=position;
                JSONObject object = new JSONObject();
                object.put("orderId",order_id);
                httpPost(Common.HOST+Common.START_SERVICE,object,from);
            }

            @Override
            public void onStartSellTicket(String service_id, String isPermit, String from,int position) {
                mPosition=position;
                JSONObject object = new JSONObject();
                object.put("serviceId",service_id);
                if (isPermit.equals(Constant.YES+"")){
                    object.put("isPermit","0");

                }else {
                    object.put("isPermit","1");

                }
                httpPost(Common.HOST+Common.START_END_SELL,object,from);
            }

            @Override
            public void onEndSellTicket(String service_id, String isPermit, String from,int position) {
                mPosition=position;
                JSONObject object = new JSONObject();
                object.put("serviceId",service_id);
                if (isPermit.equals(Constant.YES+"")){
                    object.put("isPermit","0");

                }else {
                    object.put("isPermit","1");

                }
                httpPost(Common.HOST+Common.START_END_SELL,object,from);
            }

            @Override
            public void onCancelOrder(String type, String serviceStatus, String orderId,int position,String from) {
                mPosition=position;
                cancelOrder(type, serviceStatus, orderId, from);
            }

            @Override
            public void onRouteFinish(String role, String orderId,int position, String from) {
                mPosition=position;
                JSONObject object = new JSONObject();
                object.put("role",role);
                object.put("orderId",orderId);
                httpPost(Common.HOST+Common.ROUTE_FINISH,object,from);
            }

            @Override
            public void onAlreadyInCarClick(String orderId, int position, String from) {
                mPosition=position;
                JSONObject object = new JSONObject();
                object.put("orderId",orderId);
                httpPost(Common.HOST+Common.ALREADY_IN_CAR,object,from);
            }

            @Override
            public void onReleaseNow(Order order, int position) {

                Intent intent = new Intent(PincheActivity.this, BookPayActivity.class);
                intent.putExtra("orderCode",order.getOrder_id());
                intent.putExtra("amount", order.getLoss_money()+"");
                intent.putExtra("from", Constant.HOST_PUBLISH_PAY);
                intent.putExtra("id",order.getService_id());
                intent.putExtra("alipay",Common.HOST_PUBLISH_ALIPAY);
                intent.putExtra("yivipay",Common.HOST_PUBLISH_RESTPAY);
                context.startActivity(intent);
            }
        });


    }



    private void getRelativeOrders(){
        JSONObject object = new JSONObject();
        object.put("role",identity_flag+"");
        httpPost(Common.HOST+Common.ORDER_NUM,object,"订单数量"+identity_flag);
    }

    @Override
    public void onError(Throwable ex) {
        super.onError(ex);
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        JSONObject data = obj.getJSONObject("data");
        HandleDataUtil.show(fromWhere,result);
        beanList.clear();
        if (fromWhere.equals("订单数量0")){//乘客
            //添加待付款
            JSONArray noPayList = data.getJSONArray("noPayList");
            if (noPayList!=null){
                for (int i = 0; i < noPayList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = noPayList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_PAY);
                    order.setIdentity(Constant.PASSENGER);
                    order.setForm_way(Integer.parseInt(object.getString("type")));
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setSingle_money(object.getDouble("pinServiceAccount"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setWords(object.getString("pinLeaveWords"));

                    if (object.getString("type").equals("0")){//服务
                        order.setSeats(object.getIntValue("passerRequireSeats"));
                    }else {//订单
                        order.setSeats(object.getIntValue("ownerHasSeats"));
                        order.setIcon(object.getString("ownerAvatar"));
                        order.setOwnerId(object.getString("ownerId"));
                        order.setNickname(object.getString("ownerNickName"));
                        ArrayList<String> imgList = new ArrayList<>();
                        JSONArray list = object.getJSONArray("vehicleAvatarList");
                        for (int j = 0; j < list.size(); j++) {
                            String img = list.getString(j);
                            imgList.add(img);
                        }
                        order.setImgList(imgList);
                    }
                    beanList.add(order);
                }
            }


            //添加等待接单
            JSONArray waitTakeOrdeList = data.getJSONArray("waitTakeOrdeList");
            if (waitTakeOrdeList!=null){
                for (int i = 0; i < waitTakeOrdeList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitTakeOrdeList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_TAKE);
                    order.setIdentity(Constant.PASSENGER);
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setSeats(object.getIntValue("passerRequireSeats"));

                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));

                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setPay_time(DateUtil.times(object.getLong("servicePayTime")));
                    order.setPhone(object.getString("userMobile"));
                    order.setOrder_code(object.getString("orderCode"));
                    beanList.add(order);
                }
            }


            //添加待服务
            JSONArray waitServiceList = data.getJSONArray("waitServiceList");
            if (waitServiceList!=null){
                for (int i = 0; i < waitServiceList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitServiceList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_SURVICE);
                    order.setIdentity(Constant.PASSENGER);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setLeaveOffTime(object.getLong("leaveOffTime"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setIcon(object.getString("ownerAvatar"));
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    order.setPhone(object.getString("mobile"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setNickname(object.getString("ownerNickName"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));
                    order.setSingle_money(object.getDouble("pinServiceAccount"));

                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setVehicle_type(object.getIntValue("vehicleType"));
                    order.setService_id(object.getString("serviceId"));
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);
                    beanList.add(order);
                }
            }
            //添加服务中
            JSONArray servicingList = data.getJSONArray("servicingList");
            if (servicingList!=null){
                for (int i = 0; i < servicingList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = servicingList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_SURVICE);
                    order.setIdentity(Constant.PASSENGER);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setIcon(object.getString("ownerAvatar"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setNickname(object.getString("ownerNickName"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setVehicle_type(object.getIntValue("vehicleType"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    order.setIs_in_car(object.getIntValue("isPasserOn"));
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    beanList.add(order);
                }
            }

            //添加待完结
            JSONArray waitEndList = data.getJSONArray("waitEndList");
            if (waitEndList!=null){
                for (int i = 0; i < waitEndList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitEndList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_SURVICE);
                    order.setIdentity(Constant.PASSENGER);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setIcon(object.getString("ownerAvatar"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setNickname(object.getString("ownerNickName"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setVehicle_type(object.getIntValue("vehicleType"));
                    //1 完结 0 未完结
                    if (object.getIntValue("ownerEndStatus")==0&&object.getIntValue("passerEndStatus")==1){
                        order.setIs_finished(Constant.HOST_FINISHED_PASSENGER_NO);
                    }else if (object.getIntValue("ownerEndStatus")==1&&object.getIntValue("passerEndStatus")==0){
                        order.setIs_finished(Constant.PASSENGER_FINISHED_HOST_NO);
                    }else {
                        showToast("完结状态异常");
                    }
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    beanList.add(order);
                }
            }


        }else if (fromWhere.equals("订单数量1")){//车主
            //添加待付款
            JSONArray noPayList = data.getJSONArray("noPayList");
            if (noPayList!=null){
                for (int i = 0; i < noPayList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = noPayList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_PAY);
                    order.setIdentity(Constant.HOST);
                    order.setForm_way(Integer.parseInt(object.getString("type")));
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));

                    if (object.getString("type").equals("0")) {//服务
                        order.setSeats(object.getIntValue("ownerHasSeats"));
                        order.setVehicle_type(object.getIntValue("vehicleType"));
                        order.setSingle_money(object.getDouble("pinServiceAccount"));
                    } else {//订单
                        order.setNickname(object.getString("ownerNickName"));
                        order.setIcon(object.getString("passerAvatar"));
                        order.setSeats(object.getIntValue("passerRequireSeats"));
                    }
                    beanList.add(order);
                }
            }


            //添加等待接单
            JSONArray waitTakeOrdeList = data.getJSONArray("waitTakeOrdeList");
            if (waitTakeOrdeList!=null){
                for (int i = 0; i < waitTakeOrdeList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitTakeOrdeList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_TAKE);
                    order.setIdentity(Constant.HOST);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setRest_tickets(object.getIntValue("ownerHasSeats"));//剩余座位
                    order.setPhone(object.getString("ownerMobile"));
                    order.setOrder_code(object.getString("orderCode"));

                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));
                    order.setSingle_money(object.getDouble("pinServiceAccount"));

                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setPay_time(DateUtil.times(object.getLong("servicePayTime")));
                    order.setSeats(object.getIntValue("ownerTotalSeats"));

                    if (object.getIntValue("pinSalePermit")==1){
                        order.setIs_sale_start(Constant.YES);
                    }else {
                        order.setIs_sale_start(Constant.NO);

                    }
                    beanList.add(order);
                }
            }


            //添加待服务
            JSONArray waitServiceList = data.getJSONArray("waitServiceList");
            if (waitServiceList!=null){
                for (int i = 0; i < waitServiceList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitServiceList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_SURVICE);
                    order.setIdentity(Constant.HOST);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setLeaveOffTime(object.getLong("leaveOffTime"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setIcon(object.getString("passerAvatar"));
                    order.setPhone(object.getString("mobile"));
                    order.setNickname(object.getString("passerNickName"));
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));//总价
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    order.setService_id(object.getString("serviceId"));
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);
                    beanList.add(order);
                }
            }


            //添加服务中
            JSONArray servicingList = data.getJSONArray("servicingList");
            if (servicingList!=null){
                for (int i = 0; i < servicingList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = servicingList.getJSONObject(i);
                    order.setOrder_type(Constant.IN_SURVICE);
                    order.setIdentity(Constant.HOST);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setIcon(object.getString("passerAvatar"));
                    order.setNickname(object.getString("passerNickName"));
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);

                    beanList.add(order);
                }
            }


            //添加待完结
            JSONArray waitEndList = data.getJSONArray("waitEndList");
            if (waitEndList!=null){
                for (int i = 0; i < waitEndList.size(); i++) {
                    Order order = new Order();
                    JSONObject object = waitEndList.getJSONObject(i);
                    order.setOrder_type(Constant.WAIT_FINISH);
                    order.setIdentity(Constant.HOST);
                    order.setDepart_time(DateUtil.times(object.getLong("leaveOffTime")));
                    order.setPhone(object.getString("mobile"));
                    order.setOrder_code(object.getString("orderCode"));
                    order.setOrder_id(object.getString("orderId"));
                    order.setPay_time(DateUtil.times(object.getLong("orderPayTime")));
                    order.setPay_type(object.getIntValue("orderPayType"));
                    order.setIs_timeout(object.getIntValue("orderTimeOut"));
                    order.setOwnerId(object.getString("ownerId"));
                    order.setIcon(object.getString("passerAvatar"));
                    order.setNickname(object.getString("passerNickName"));
                    order.setSeats(object.getIntValue("passerRequireSeats"));
                    order.setStart_time(DateUtil.times(object.getLong("pinCreateTime")));
                    order.setEnd_place(object.getString("pinEndPoint"));
                    order.setWords(object.getString("pinLeaveWords"));
                    order.setLoss_money(object.getDouble("pinLoseAccount"));
                    order.setService_code(object.getString("pinServiceCode"));
                    order.setStyle(object.getIntValue("pinServiceKinds"));
                    order.setStart_place(object.getString("pinStartPoint"));
                    order.setDistance(object.getDouble("pinTotalDistance"));
                    order.setService_id(object.getString("serviceId"));
                    order.setTotal_money(object.getDouble("pinTotalAccount"));
                    order.setWho_created(object.getIntValue("orderCreateBy"));
                    //1 完结 0 未完结
                    if (object.getIntValue("ownerEndStatus")==0&&object.getIntValue("passerEndStatus")==1){
                        order.setIs_finished(Constant.HOST_FINISHED_PASSENGER_NO);
                    }else if (object.getIntValue("ownerEndStatus")==1&&object.getIntValue("passerEndStatus")==0){
                        order.setIs_finished(Constant.PASSENGER_FINISHED_HOST_NO);
                    }else {
                        showToast("完结状态异常");
                    }
                    ArrayList<String> imgList = new ArrayList<>();
                    JSONArray list = object.getJSONArray("vehicleAvatarList");
                    for (int j = 0; j < list.size(); j++) {
                        String img = list.getString(j);
                        imgList.add(img);
                    }
                    order.setImgList(imgList);
                    beanList.add(order);
                }
            }
        }else {
            getRelativeOrders();
        }
        adapter.notifyDataSetChanged();
        if (beanList.size()==0){
           // listView.setVisibility(View.GONE);
            rl_building.setVisibility(View.VISIBLE);
        }else {
           // listView.setVisibility(View.VISIBLE);
            rl_building.setVisibility(View.GONE);
        }
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_passenger_tab = (TextView)findViewById(R.id.tv_passenger_tab);
        tv_host_tab = (TextView)findViewById(R.id.tv_host_tab);
        tv_release = (TextView)findViewById(R.id.tv_release);
        tabLayout = (TabLayout)findViewById(R.id.tablayout);
        tv_start_city = (TextView)findViewById(R.id.tv_start_city);
        tv_end_city = (TextView)findViewById(R.id.tv_end_city);
        iv_change_direction = (ImageView)findViewById(R.id.iv_change_direction);
        ll_choose_date = (LinearLayout)findViewById(R.id.ll_choose_date);
        tv_date = (TextView)findViewById(R.id.tv_date);
        tv_week = (TextView)findViewById(R.id.tv_week);
        tv_start_time = (TextView)findViewById(R.id.tv_start_time);
        tv_end_time = (TextView)findViewById(R.id.tv_end_time);
        btn_search = (Button)findViewById(R.id.btn_search);
        listView = (MyListview) findViewById(R.id.listView);
        ll_exchange_pos = (LinearLayout)findViewById(R.id.ll_exchange_pos);
        rl_left = (RelativeLayout)findViewById(R.id.rl_left);
        rl_right = (RelativeLayout)findViewById(R.id.rl_right);
        tv_order = (TextView)findViewById(R.id.tv_order);
        iv_left = (ImageView)findViewById(R.id.iv_left);
        iv_right = (ImageView)findViewById(R.id.iv_right);
        refreshView = (PullToRefreshScrollView)findViewById(R.id.refresh_view);
        rl_building = (RelativeLayout)findViewById(R.id.rl_building);


    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_passenger_tab.setOnClickListener(this);
        tv_host_tab.setOnClickListener(this);
        tv_release.setOnClickListener(this);
        ll_choose_date.setOnClickListener(this);
        iv_change_direction.setOnClickListener(this);
        tv_start_time.setOnClickListener(this);
        tv_end_time.setOnClickListener(this);
        tv_release.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        tv_order.setOnClickListener(this);
        rl_left.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        listView.setOnItemClickListener(this);


        refreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getRelativeOrders();
                refreshView.onRefreshComplete();
            }
        });
    }
    /*
    * 取消订单
    * */
    private void cancelOrder(String type, String serviceStatus, String orderId, String from) {
        JSONObject object = new JSONObject();
        object.put("type",type);
        object.put("serviceStatus",serviceStatus);
        object.put("orderId",orderId);
        httpPost(Common.HOST+Common.CANCEL_ORDER,object,from);
    }



    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            //选择开始城市
            case R.id.rl_left:
                startActivityForResult(new Intent(this,SelectPositionActivity.class),Constant.REQUEST_CODE_START_PLACE);

                break;
            //选择终点城市
            case R.id.rl_right:
                startActivityForResult(new Intent(this,SelectPositionActivity.class),Constant.REQUEST_CODE_END_PLACE);

                break;
            //交换出发地和目的地
            case R.id.iv_change_direction:
                textAnim();
                break;
            //选择日期
            case R.id.ll_choose_date:
                //WidgetUtil.showDatePicker(this,tv_date);
                showIOSDate(tv_date, TimePickerView.Type.YEAR_MONTH_DAY,"请选择出发日期",1);
                break;
            //选择开始时间
            case R.id.tv_start_time:
                //WidgetUtil.showTimePicker(this, tv_start_time);
                showIOSDate(tv_start_time, TimePickerView.Type.HOURS_MINS,"请选择出发时间",0);
                break;
            //选择结束时间
            case R.id.tv_end_time:
                //WidgetUtil.showTimePicker(this, tv_end_time);
                showIOSDate(tv_end_time, TimePickerView.Type.HOURS_MINS,"请选择截止时间",0);
                break;
            case R.id.tv_host_tab:
                tv_host_tab.setTextColor(getResources().getColor(R.color.colorAb));
                tv_passenger_tab.setTextColor(getResources().getColor(R.color.host_selected_color));
                iv_left.setVisibility(View.GONE);
                iv_right.setVisibility(View.VISIBLE);
                //白色选中,刷新下方列表数据
                identity_flag=Constant.HOST;
                getRelativeOrders();
                break;
            case R.id.tv_passenger_tab:
                tv_host_tab.setTextColor(getResources().getColor(R.color.host_selected_color));
                tv_passenger_tab.setTextColor(getResources().getColor(R.color.colorAb));
                iv_left.setVisibility(View.VISIBLE);
                iv_right.setVisibility(View.GONE);
                //白色选中，刷新列表
                identity_flag=Constant.PASSENGER;
                getRelativeOrders();
                break;
            case R.id.btn_search:
                showToast(this,tv_start_city.getText().toString());
                Order order = getOrderParam();
                if (TextUtils.isEmpty(order.getStart_place())){
                    showToast("请输入出发地点");
                    return;
                }
                if (TextUtils.isEmpty(order.getEnd_place())){
                    showToast("请输入目的地");
                    return;
                }

                Intent intent;
                if (identity_flag==Constant.PASSENGER){

                    if (order.getStyle()==Constant.LONG_HURRY){
                        showToast("搜索车主发布的长途急件");
                        return;
                    }else {
                        intent=new Intent(this,HostOrdersActivity.class);
                        intent.putExtra("search_param",order);
                    }

                }else {
                    if (order.getStyle()==Constant.LONG_HURRY){
                        showToast("搜索乘客发布的长途急件");
                        return;
                    }else {
                        intent=new Intent(this,PassengerOrdersActivity.class);
                        intent.putExtra("search_param",order);
                    }

                }
                startActivity(intent);
                break;
            case R.id.tv_release:
                Intent intentRelease = new Intent(this, ReleaseActivity.class);
                intentRelease.putExtra("identity",identity_flag);
                startActivity(intentRelease);
                break;

        }
    }

    @NonNull
    private Order getOrderParam() {
        int type = Constant.LONG;
        Order order = new Order();
        order.setOrder_publisher(identity_flag);
        switch (tabLayout.getSelectedTabPosition()){
            case 0:
                type=Constant.LONG;
                break;
            case 1:
                type=Constant.CITY;
                break;
            case 2:
                type=Constant.TRAVEL;
                break;
            case 3:
                type=Constant.LONG_HURRY;
                break;
        }
        order.setStyle(type);
        order.setStart_place(tv_start_city.getText().toString());
        order.setEnd_place(tv_end_city.getText().toString());
        order.setDepart_time(HandleDataUtil.withoutChinese(tv_date.getText().toString()));
        order.setStart_time(order.getDepart_time()+" "+tv_start_time.getText().toString());
        order.setEnd_time(order.getDepart_time()+" "+tv_end_time.getText().toString());
        return order;
    }

    /**
     * 左边tv的镜像view
     */
    private ImageView copyViewLeft;
    /**
     * 右边tv的镜像view
     */
    private ImageView copyViewRight;

    /**
     * 获取tv的属性,计算偏移量,
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void textAnim() {

        //获取tv控件距离父控件的位置
        int leftRight = tv_start_city.getRight();
        int rightLeft = tv_end_city.getLeft();

        //包裹右侧tv距离父控件的距离
        int rlRight = rl_right.getRight();
        int rlLeft = rl_right.getLeft();
        //在哪里设的padding就要用哪个控件来获取padding值
        int paddingStart = ll_exchange_pos.getPaddingStart();

        Log.d("AddressActivity", "paddingStart:" + paddingStart);

        //左侧textview需要移动的距离
        int leftOffset = rlRight - leftRight - paddingStart;
        //右侧textview需要移动的距离
        int rightOffset = rlLeft + rightLeft - paddingStart;

        //创建出镜像view
        createCopyView();

        //隐藏掉两边的tv
        tv_start_city.setVisibility(View.INVISIBLE);
        tv_end_city.setVisibility(View.INVISIBLE);

        //开启镜像view的动画
        leftAnim(leftOffset,mLeftLocation[0]);
        rightAnim(rightOffset,mRightLocation[0]);
    }

    /**
     * 创建镜像view
     */
    private void createCopyView(){
        mLeftLocation = new int[2];
        mRightLocation = new int[2];
        //获取相对window的坐标
        tv_start_city.getLocationInWindow(mLeftLocation);
        tv_end_city.getLocationInWindow(mRightLocation);

        //获取左边tv的缓存bitmap
        tv_start_city.setDrawingCacheEnabled(true);
        mLeftCacheBitmap = Bitmap.createBitmap(tv_start_city.getDrawingCache());
        tv_start_city.destroyDrawingCache();
        //获取右边tv的缓存bitmap
        tv_end_city.setDrawingCacheEnabled(true);
        mRightCacheBitmap = Bitmap.createBitmap(tv_end_city.getDrawingCache());
        tv_end_city.destroyDrawingCache();

        //创建出两个镜像view
        copyViewLeft = createCopyView(mLeftLocation[0], mLeftLocation[1], mLeftCacheBitmap);
        copyViewRight = createCopyView(mRightLocation[0], mRightLocation[1], mRightCacheBitmap);
        //释放bitmap资源...这我不确定是不是这么做
        mLeftCacheBitmap = null;
        mRightCacheBitmap = null;
    }

    /**
     * 左侧镜像view的动画
     * @param offset    偏移量
     * @param defX      原始位置的x
     */
    private void leftAnim(int offset, final int defX){
        ValueAnimator leftAnimV = ValueAnimator.ofInt(0,offset);
        leftAnimV.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int animatedValue = (int) valueAnimator.getAnimatedValue();
                WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) copyViewLeft.getLayoutParams();
                //往右边移动所以x是变大的
                layoutParams.x = defX + animatedValue;
                mWindowManager.updateViewLayout(copyViewLeft,layoutParams);
            }
        });
        leftAnimV.setDuration(400);
        leftAnimV.start();
        //左侧动画监听
        leftAnimV.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //改变值
                String s = tv_start_city.getText().toString();
                tv_start_city.setText(tv_end_city.getText().toString());
                tv_end_city.setText(s);
                tv_start_city.setVisibility(View.VISIBLE);
                mWindowManager.removeView(copyViewLeft);
                copyViewLeft = null;
            }
        });
    }

    /**
     * 右侧镜像view动画
     * @param offset    偏移量
     * @param defX      原始位置的x
     */
    private void rightAnim(int offset, final int defX){
        ValueAnimator rightAnimV = ValueAnimator.ofInt(0,offset);
        rightAnimV.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int animatedValue = (int) valueAnimator.getAnimatedValue();
                WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) copyViewRight.getLayoutParams();
                layoutParams.x = defX - animatedValue;
                mWindowManager.updateViewLayout(copyViewRight,layoutParams);
            }
        });
        rightAnimV.setDuration(400);
        rightAnimV.start();
        rightAnimV.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                tv_end_city.setVisibility(View.VISIBLE);
                mWindowManager.removeView(copyViewRight);
                copyViewRight = null;
            }
        });
    }

    /**
     * 创建镜像view
     *
     * @param x
     * @param y
     * @param bitmap
     */
    private ImageView createCopyView(int x, int y, Bitmap bitmap) {
        WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.format = PixelFormat.TRANSLUCENT;            //图片之外其他地方透明
        mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
        mLayoutParams.x = x;   //设置imageView的原点
        mLayoutParams.y = y - getStatusHeight(this);
        mLayoutParams.alpha = 1f;                                //设置透明度
        mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        ImageView copyView = new ImageView(this);
        copyView = new ImageView(this);
        copyView.setImageBitmap(bitmap);
        mWindowManager.addView(copyView, mLayoutParams);   //添加该iamgeView到window
        return copyView;
    }

    /**
     * 获取状态栏的高度
     * @param context
     * @return
     */
    private static int getStatusHeight(Context context) {
        int statusHeight = 0;
        Rect localRect = new Rect();
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight) {
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
                statusHeight = context.getResources().getDimensionPixelSize(i5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = null;
        Order order = beanList.get(i);
        if (order.getIdentity()==Constant.PASSENGER){
            intent = new Intent(getActivity(),PincheOrderDetailActivity.class);
        }else {
            intent=new Intent(getActivity(),PincheOrderDetailHostActivity.class);
        }
        intent.putExtra("order_type",order.getOrder_type());
        intent.putExtra("go_type",order.getStyle());
        intent.putExtra("order",order);
        intent.putExtra("position",i);
        intent.putExtra("order_id",order.getOrder_id());
        if (order.getOrder_type()==Constant.WAIT_PAY){
            if (order.getForm_way()==Constant.RELEASE){
                if (!TextUtils.isEmpty(order.getService_id())){
                    intent.putExtra("serviceId",order.getService_id());
                }else {
                    showToast("serviceId为空");
                    return;
                }
            }else {
                intent.putExtra("serviceId","");
            }
        }else if (order.getOrder_type()==Constant.WAIT_TAKE){
            if (!TextUtils.isEmpty(order.getService_id())){
                intent.putExtra("serviceId",order.getService_id());
            }else {
                showToast("serviceId为空");
                return;
            }
        }else {
            intent.putExtra("serviceId","");
        }
        startActivityForResult(intent,Constant.REQUEST_CODE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data!=null){
            //选择地点回调
            if (requestCode== Constant.REQUEST_CODE_START_PLACE&&resultCode==Constant.RESULT_CODE_PLACE){
                Tip tip = data.getParcelableExtra("selected_tip");
                if (tip.getName().contains("市")){
                    String s = tip.getName().replace("市", "");
                    tv_start_city.setText(s);
                }else {
                    tv_start_city.setText(tip.getName());
                }
            }else if (requestCode== Constant.REQUEST_CODE_END_PLACE&&resultCode==Constant.RESULT_CODE_PLACE){
                Tip tip = data.getParcelableExtra("selected_tip");
                if (tip.getName().contains("市")){
                    String s = tip.getName().replace("市", "");
                    tv_end_city.setText(s);
                }else {
                    tv_end_city.setText(tip.getName());
                }
            }else if (requestCode==Constant.REQUEST_CODE){//订单详情回调
                getRelativeOrders();
            }

        }
    }
}
